package com.example.smartcity_poverty.ui.AllService.LogisticsQuery

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.context
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.LogisticsQueryBean.LqBannerModel
import com.example.smartcity_poverty.logic.ServiceBean.LogisticsQueryBean.LqMoreModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.LogisticsQuery.activity.LqDanHaoActivity
import com.example.smartcity_poverty.ui.AllService.LogisticsQuery.adapter.LqListAdapter
import com.example.smartcity_poverty.ui.AllService.LogisticsQuery.adapter.LqMoreAdapter
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.activity_logistics_query.*
import kotlinx.coroutines.Dispatchers

/**
 * 物流查询
 */
class LogisticsQueryActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_logistics_query)
        title = "物流查询"

        runThread()

        // 实现搜索单号
        lqSeek.setOnEditorActionListener { textView, i, keyEvent ->
            if (i == EditorInfo.IME_ACTION_SEARCH) {
                if (lqSeek.text.toString().isNotEmpty()) {
                    startActivity(Intent(this, LqDanHaoActivity::class.java).apply {
                        putExtra("content", lqSeek.text.toString())
                    })
                } else "你还没有输入单号！".show()
            }
            false
        }
    }


    // 管理协程
    private fun runThread() = coroutine {
        // Banner
        setBanner()

        // More
        setMore()

        // List
        setList()
    }

    /**
     * TODO 轮播图
     */
    private var arrayBanner = ArrayList<LqBannerModel.DataBean>()
    private suspend fun setBanner() {
        if (getToken().isNotEmpty()) {
            api.getLqBanner(getToken()).apply {
                if (this.code == 200) {
                    arrayBanner.addAll(this.data!!)
                    runOnUiThread {
                        lqBanner.adapter =
                            object : BannerImageAdapter<LqBannerModel.DataBean>(arrayBanner) {
                                override fun onBindView(
                                    p0: BannerImageHolder?,
                                    p1: LqBannerModel.DataBean?,
                                    p2: Int,
                                    p3: Int
                                ) {
                                    glide(url + p1?.imgUrl, p0?.imageView!!)
                                }
                            }
                        lqBanner.setBannerRound(25f)
                        lqBanner.addBannerLifecycleObserver(this@LogisticsQueryActivity).indicator =
                            CircleIndicator(context)
                    }
                } else runOnUiThread { this.msg?.show() }
            }
        } else runOnUiThread { "请先登录！".show() }
    }


    /**
     * More
     */
    private var arrayMore = ArrayList<LqMoreModel.DataBean>()
    private suspend fun setMore() {
        if (getToken().isNotEmpty()) {
            api.getLqMore(getToken()).apply {
                if (this.code == 200) {
                    arrayMore.addAll(this.data!!)
                    runOnUiThread {
                        lqMoreAllRv.apply {
                            this.isNestedScrollingEnabled = false
                            this.layoutManager = GridLayoutManager(context, 5)
                            this.adapter = LqMoreAdapter(15, arrayMore, R.layout.re_lq_iten_more)
                            this.adapter?.notifyItemChanged(arrayMore.size)
                        }
                    }
                }
            }
        } else runOnUiThread { "请先登录！".show() }
    }


    /**
     * 其他公司列表
     */
    private var arrayList = ArrayList<LqMoreModel.DataBean>()
    private suspend fun setList() {
        if (getToken().isNotEmpty()) {
            api.getLqMore(getToken()).apply {
                if (this.code == 200) {
                    arrayList.addAll(this.data!!)
                    runOnUiThread {
                        lqListRv.apply {
                            this.isNestedScrollingEnabled = false
                            this.layoutManager = LinearLayoutManager(context)
                            this.adapter = LqListAdapter(arrayList, R.layout.re_lq_item_list)
                            this.adapter?.notifyItemChanged(arrayList.size)
                        }
                    }
                }
            }
        } else runOnUiThread { "请先登录！".show() }
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}